// 导入api接口
import { cpList, delImgMaintenance, repairAdd, repairClassList, repairDelete, repairDxList, repairFactoryList, repairImgList, repairList, repairUpdate, repairXxList, sjList } from '@/api/api'
import { dateRevert, imgFilter } from '@/utils/businessTools'
import configJs from '@/utils/configJs'
import defaultData from './data'
var data = JSON.parse(JSON.stringify(defaultData))
function notNull(arr) {return arr.reduce((total,item) => {total = true/*total && (item !== '' && item !== null)*/;return total},true)} // 判断数组元素都不为null或''
function plusArr(arr) {return arr.reduce((total,item) => {total = Number(total) + Number(item);return total},0)}
var adapter = {
	data,
	methods: {
		// ...mapActions(['logOut']),
		/**
		 * 初始化
		 */
		initPage(){
			//还原页面对象
			configJs.revert(data, defaultData);
			this.carSearchObj[0].cph = this.$store.state.app.navcp; // 写入公共车牌号
		},
		
		/**
		 * 查询员工列表
		 */
		async search(val) {
			if (!this.$store.getters.pageAuth.authoritySearch) {
				return
			}
			this.revertActive() // 重置选择行
			let params = {...this.carSearchObj[0]}
			// 处理时间
			params.wxsj = dateRevert(params.wxsj)
			typeof val != 'number' && (val = 1)
			params.page = val
			const res = await repairList(params)
			if (res.code === 0) {
			  this.page = res.data
				this.pageTotal = res.total
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 刷新
		 */
		async refresh() {
			this.carSearchObj = [{
				cph: '',
				wxsj: ['',''],
				xlcd: '',
				wxlb: '',
				wxdx: '',
				wxzx: '',
				gg: '',
				jbh: '',
				jtqx: '',
				xbh: '',
				sxf: '',
				zjf: '',
				spf: '',
				gsf: '',
				qtf: '',
				sl: '',
				dj: '',
				je: '',
				sfrz: '',
				fzsj: '',
				bz: ''
			}]
			this.search()
		},

		addListComputed(record) {
			// 计算金额
			let sxf = record.sxf
			let zjf = record.zjf
			let spf = record.spf
			let gsf = record.gsf
			let qtf = record.qtf
			let sl = record.sl
			let dj = record.dj
			let je = record.je
			if (notNull([sxf,zjf,spf,gsf,qtf])) {
				je = plusArr([sxf,zjf,spf,gsf,qtf])
			}
			if (notNull([sl,dj])){
				let fee = (sl*dj).toFixed(2)
				if (notNull([je,fee])){
					je = plusArr([je,fee])
				}
			}
			this.$set(record,'je',je)
		},
		addComputed() {
			this.editListComputed(this.addCar[0])
		},


		/**
		 * 增加
		 */
		add() {
			this.resetAdd()
			this.addVisi = true
		},

		/**
		 * 重置增加弹窗
		 */
		resetAdd() {
			this.addCar = [{
				id: '',
				cph: '',
				wxsj: '',
				xlcd: '',
				wxlb: '',
				wxdx: '',
				wxzx: '',
				gg: '',
				jbh: '',
				jtqx: '',
				xbh: '',
				sxf: '',
				zjf: '',
				spf: '',
				gsf: '',
				qtf: '',
				sl: '',
				dj: '',
				je: '',
				sfrz: '',
				fzsj: '',
				bz: ''
			}]
		},

		/**
		 * 增加点击
		 */
		async addClick() {
			// const valid = await this.$refs['addStaff'].validate() // 校验
			// if(valid) {
				const res = await repairAdd(this.addCar[0])
				if (res.code === 0) {
					this.$message('添加成功')
					this.addVisi = false
					this.search()
				} else {
					this.$message(res.msg)
				}
			// }
		},

		editListComputed(record) {
			// 计算金额
			let sxf = record.sxf
			let zjf = record.zjf
			let spf = record.spf
			let gsf = record.gsf
			let qtf = record.qtf
			let sl = record.sl
			let dj = record.dj
			let je = record.je
			if (notNull([sxf,zjf,spf,gsf,qtf])) {
				je = plusArr([sxf,zjf,spf,gsf,qtf])
			}
			if (notNull([sl,dj])){
				let fee = (sl*dj).toFixed(2)
				if (notNull([je,fee])){
					je = plusArr([je,fee])
				}
			}
			this.$set(record,'je',je)
		},
		editComputed() {
			this.editListComputed(this.editCar)
		},

		 /**
			* 修改
		  */
		edit(car) {
			this.editCar = {...car}
			this.editVisi = true
		},

		/**
		 * 日期转换
		 */
		dateFormat(date) {
			if ('' === date) {
				return date
			} else {
				if (typeof date == 'String') {
					let arr = date.split('_')
					return arr
				} else {
					if (Array.isArray(date) && date.length == 2) {
						return date[0] + '_' + date[1]
					} else {
						return date
					}
				}
			}
		},

		/**
		 * 修改点击
		 */
		async editClick() {
			const res = await repairUpdate(this.editCar)
			if (res.code === 0) {
				this.$message('修改成功')
				this.editVisi = false
				this.search()
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 删除
		 */
		async deleteClick(id) {
			await this.$confirm('确认删除?', '', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
			const res = await repairDelete(id)
			if (res.code === 0) {
				this.$message('修改成功')
				this.editVisi = false
				this.search()
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 点击行
		 */
		rowClick(row, column, e) {
			if (row.id != this.activeCar.id) {
				this.activeCar = row
				this.getImgList()
			}
		},

		/**
		 * 重置选择行
		 */
		revertActive() {
			this.activeCar = {
				id: '',
				bm: '',
				xz: '',
				xm: '',
				nl: '',
				zwzz: '',
				sfzh: '',
				sfzyxq: '',
				zz: '',
				lxdh: '',
				rzrq: '',
				cyzgzyxq: '',
				cyzgzbh: '',
				dabh: '',
		
				zjcx: '',
				jszcclzrq: '',
				jszyxq: '',
				sfba: '',
				bacl: '',
				bz: ''
			}
			this.activeImgList = []
		},

		/**
		 * 日期选择
		 */
		 datePick() {
			 console.error('datepick')
			 console.log(arguments)
			 return 1
		 },

		/**
		 * 维修厂店列表
		 */
		async getListRepairFactory() {
			const res = await repairFactoryList()
			if (res.code === 0) {
			  this.repairFactoryList = res.data.list
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 维修项目列表
		 */
		 async getListRepairClass() {
			const res = await repairClassList()
			if (res.code === 0) {
			  this.repairClassList = res.data.list
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 维修大项列表
		 */
		 async getListRepairDx() {
			const res = await repairDxList()
			if (res.code === 0) {
			  this.repairDxList = res.data.list
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 维修小项列表
		 */
		 async getListRepairXx() {
			const res = await repairXxList()
			if (res.code === 0) {
			  this.repairXxList = res.data.list
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 车牌列表
		 */
		 async getListCp() {
			const res = await cpList()
			if (res.code === 0) {
			  this.cpList = res.data.list
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 司机列表
		 */
		 async getListSj() {
			const res = await sjList()
			if (res.code === 0) {
			  this.sjList = res.data.list
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 获取图片列表
		 */
		async getImgList() {
			const res = await repairImgList(this.activeCar.id)
			if (res.code === 0) {
				this.activeImgList = imgFilter(res.data.list)
			}
		},

		/**
		 * 删除图片
		 */
		async imgDel(fileName) {
			await this.$confirm('确认删除?', '', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
			const res = await delImgMaintenance(fileName)
			if (res.code === 0) {
				this.$message('删除成功')
				this.getImgList()
			} else {
				this.$message(res.msg)
			}
		}
	},
	computed: {
	}
}
export default adapter
